package io.mft.commons.util.xml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.util.ObjectUtil;

public class IOUtil {
	static Logger logger=LoggerFactory.getLogger(IOUtil.class);
  public static String unifiedWdLxPath(String path){
	  path=path.replace("/", File.separator);
	  path=path.replace("\\", File.separator);
	  return path;
  }
  public static boolean inPutStreamTofile(InputStream inStream, String targetfile) {
      FileOutputStream fs = null;
      try {
          File target = new File(targetfile);
          File dir= target.getParentFile();
          if (!dir.exists()) {
              dir.mkdirs();
          }
          if (target.exists()){target.delete();}
          target.createNewFile();
          fs = new FileOutputStream(target);
          byte[] buffer = new byte[1024];
          int byteread = 0;
          while ((byteread = inStream.read(buffer)) != -1) {
              fs.write(buffer, 0, byteread);
          }
          return true;
      } catch (Exception e) {
    	  logger.error("", e);
          return false;
      } finally {
              if (fs != null) {
                  try {
                      fs.close();
                  } catch (IOException e) {
                	  logger.error("", e);
                  }
              }
              if (inStream != null) {
                  try {
                      inStream.close();
                  } catch (IOException e) {
                     logger.error("", e);
                  }
              }
      }
  }

    public static boolean copyfile(File source, String targetfile) {
        try {
            InputStream inStream = new FileInputStream(source);
            return inPutStreamTofile(inStream, targetfile);
        } catch (FileNotFoundException e) {
        	logger.error("", e);
            return false;
        }

    }

    public static String lineToDotSplitPath(String lineSplitPath){
        String t=lineSplitPath.replace(File.separator, ".").replace("/", ".");
        if (".".equals(t.substring(t.length()-1))) {
            t=t.substring(0,t.length()-1);
        }
        return  t;
    }

    public static String dotToLineSplitPath(String dotSplitPath){
        String t=dotSplitPath.replace(".",File.separator);
        return  t;
    }

    public static String toHtmlLinefeed(String text) {
        if (!ObjectUtil.isEmpty(text)) {
            text = text.replace("\n", "<br/>");
        }
        return text;
    }

    public static String filterHtml(String string) {
        String str = string.replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", "").replaceAll("</[a-zA-Z]+[1-9]?>", "");
        return str;
    }

	public static  List<String> readTextfile(String filePath,int start,int end) {
		BufferedReader br=null;
		InputStreamReader reader=null;
		FileInputStream stream=null;
		try {
			stream=new FileInputStream(filePath);
			reader=new InputStreamReader(stream);
			br = new BufferedReader(reader);
			List<String>  list=new ArrayList<String>();
			int i=0;
		
			for (String line = br.readLine(); line != null; line = br.readLine()) {
				if (i>=start&&i<=end) {
					list.add(line);
				}
				if (i>end) {
					break;
				}
				i++;
			}
			br.close();
			return list;
		} catch (Exception e) {
			logger.error("", e);
			return null;
		}finally{
			try {
				if (br!=null) {
					br.close();
				}
			} catch (Exception e) {
				logger.error("", e);
			}
			try {
				if (reader!=null) {
					reader.close();
				}
			} catch (Exception e) {
				logger.error("", e);
			}
		}
	}
	public static int readTextfileTotalLine(String filePath) {
		BufferedReader br=null;
		InputStreamReader reader=null;
		FileInputStream stream=null;
		try {
			stream=new FileInputStream(filePath);
			reader=new InputStreamReader(stream);
			br = new BufferedReader(reader);
			int i=0;
			for (String line = br.readLine(); line != null; line = br.readLine()) {
				i++;
			}
			br.close();
			return i;
		} catch (Exception e) {
			logger.error("", e);
			return -1;
		}finally{
			try {
				if (br!=null) {
					br.close();
				}
			} catch (Exception e) {
				logger.error("", e);
			}
			try {
				if (stream!=null) {
					stream.close();
				}
			} catch (Exception e) {
				logger.error("", e);
			}
			try {
				if (reader!=null) {
					reader.close();
				}
			} catch (Exception e) {
				logger.error("", e);
			}
		}
	}
	/**
	public interface ResourceAsStreamAction{
		
		default InputStream getResourceAsStream(ClassLoader classLoader,String path) {
			return classLoader.getResourceAsStream(path);
		}
	}
	public static InputStream getResourceAsStream(ClassLoader classLoader,ResourceAsStreamAction action,String path) throws MalformedURLException, IOException {
		URL url=classLoader.getResource(".");
		String f=url.getPath()+path;
		File file=new File(f);
		if (file.exists()&&file.isFile()) {
			InputStream inputStream=file.toURI().toURL().openStream();
			return inputStream;
		}else {
			InputStream inputStream=action.getResourceAsStream(classLoader,path);
			return inputStream;
		}
	}
	
	public static InputStream getResourceAsStream(ClassLoader classLoader,String path) throws MalformedURLException, IOException {
		return getResourceAsStream(classLoader, new ResourceAsStreamAction() {
		}, path);
	}**/
}
